package com.yupi.yuoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.exception.BusinessException;
import com.yupi.yuoj.model.entity.CommentLike;
import com.yupi.yuoj.model.entity.ContentComment;
import com.yupi.yuoj.model.entity.User;
import com.yupi.yuoj.service.CommentLikeService;
import com.yupi.yuoj.service.ContentCommentService;
import com.yupi.yuoj.mapper.CommentLikeMapper;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author admin
* @description 针对表【comment_like(评论点赞表)】的数据库操作Service实现
* @createDate 2025-11-01 21:29:01
*/
@Service
public class CommentLikeServiceImpl extends ServiceImpl<CommentLikeMapper, CommentLike>
    implements CommentLikeService{

    @Resource
    private ContentCommentService contentCommentService;

    /**
     * 评论点赞
     *
     * @param commentId
     * @param loginUser
     * @return
     */
    @Override
    public int doCommentLike(long commentId, User loginUser) {
        // 判断实体是否存在，根据类别获取实体
        ContentComment contentComment = contentCommentService.getById(commentId);
        if (contentComment == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        // 是否已点赞
        long userId = loginUser.getId();
        // 每个用户串行点赞
        // 锁必须要包裹住事务方法
        CommentLikeService commentLikeService = (CommentLikeService) AopContext.currentProxy();
        synchronized (String.valueOf(userId).intern()) {
            return commentLikeService.doCommentLikeInner(userId, commentId);
        }
    }

    /**
     * 封装了事务的方法
     *
     * @param userId
     * @param commentId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int doCommentLikeInner(long userId, long commentId) {
        CommentLike commentLike = new CommentLike();
        commentLike.setUser_id(userId);
        commentLike.setComment_id(commentId);
        QueryWrapper<CommentLike> commentLikeQueryWrapper = new QueryWrapper<>(commentLike);
        CommentLike oldCommentLike = this.getOne(commentLikeQueryWrapper);
        boolean result;
        // 已点赞
        if (oldCommentLike != null) {
            result = this.remove(commentLikeQueryWrapper);
            if (result) {
                // 点赞数 - 1
                ContentComment contentComment = contentCommentService.getById(commentId);
                Integer likeCount = contentComment.getLike_count();
                if (likeCount != null && likeCount > 0) {
                    ContentComment updateComment = new ContentComment();
                    updateComment.setId(commentId);
                    updateComment.setLike_count(likeCount - 1);
                    contentCommentService.updateById(updateComment);
                }
                return -1;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        } else {
            // 未点赞
            result = this.save(commentLike);
            if (result) {
                // 点赞数 + 1
                ContentComment contentComment = contentCommentService.getById(commentId);
                Integer likeCount = contentComment.getLike_count();
                if (likeCount == null) {
                    likeCount = 0;
                }
                ContentComment updateComment = new ContentComment();
                updateComment.setId(commentId);
                updateComment.setLike_count(likeCount + 1);
                contentCommentService.updateById(updateComment);
                return 1;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
    }

}




